package com.singlemountain.gcommerce.web.shopByCategory;


import com.singlemountain.gcommerce.domain.product.Productd;
import com.singlemountain.gcommerce.domain.user.Userd;
import com.singlemountain.gcommerce.web.BaseController;
import com.singlemountain.gcommerce.web.BaseValidators;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Set;

@Controller
public class ProductListController extends BaseController<Userd, BaseValidators> {
    public final String URL_SHOP_BY_CATEGORY_PRODUCT_LIST = "/shopByCategoryProductList/{pageNo}/{maxResult}";
    public final String VIEW_SHOP_BY_CATEGORY_PRODUCT_LIST = "view_shopByCategoryProductList";

    @RequestMapping(value = URL_SHOP_BY_CATEGORY_PRODUCT_LIST, method = RequestMethod.GET)
    public String setUpPage(ModelMap modelMap,@PathVariable("pageNo") int pageNo,
                            @PathVariable("maxResult") int maxResult) {
       // maxResult = 16;
        int totalNum= (int) productdService.countAllProductds();
        int firstResult=(pageNo-1)*maxResult;
        List<Productd> productdList=productdService.findProductdEntries(firstResult,maxResult);

        int totalPage=totalNum%maxResult>0?totalNum/maxResult+1:totalNum/maxResult;
        modelMap.addAttribute("pageNo",pageNo);
        modelMap.addAttribute("totalNum",totalNum);
        modelMap.addAttribute("totalPage",totalPage);
        modelMap.addAttribute("firstResult",firstResult);
        modelMap.addAttribute("maxResult",maxResult);
        modelMap.addAttribute("productdList",productdList);

        return VIEW_SHOP_BY_CATEGORY_PRODUCT_LIST;
    }




}
